<!-- 本示例未包含完整css，获取外链css请参考上文，在hello uni-app项目中查看 -->
<template>
    <view>
        <view>
            <br>
            <uni-row>
                <uni-col :span="1" ><text decode>{{emptyText}}</text></uni-col>
                <uni-col :span="22">
                    <uni-forms :model="formData" :rules="rules" ref="form" label-width="80px">
                        <uni-forms-item label="图书编号" name="bookCode">
                            <uni-easyinput type="text" v-model="formData.bookCode" placeholder="请输入图书编号" />
                        </uni-forms-item>
                    </uni-forms>
                    <button @click="formSubmit" hover-class="is-hover" type="primary">归还</button>
                </uni-col>
                <uni-col :span="1"> </uni-col>
            </uni-row>
        </view>
    </view>
</template>


<script>
    import api from '@/common/vmeitime-http/'
    export default {
        onLoad(options) {
            this.formData.siteId = options.siteId
        },
        data() {
            return {
                emptyText:'&ensp;',
                formData: {
                    siteId: '',
                    bookCode: '',
                },
                rules: {
                    bookCode: {
                        rules: [{
                            required: true,
                            errorMessage: '请输入图书编号',
                        }, ]
                    },
                }
            }
        },

        methods: {
            formSubmit: function(e) {
                this.$refs.form.validate().then(res => {
                    api.backBook(this.formData).then((res)=>{
                    	if(res.data.code===200){
                    	    uni.showToast({
                    	        title: '图书归还成功',
                    	        //将值设置为 success 或者直接不用写icon这个参数
                    	        icon: 'success',
                    	        //显示持续时间为 2秒
                    	        duration: 1000
                    	    })
                    	    setTimeout(function() {
                    	    	uni.navigateBack()}, 1000);							
                    	}else{
                            uni.showToast({
                                title: res.data.msg,
                                //将值设置为 success 或者直接不用写icon这个参数
                                icon: 'error',
                                //显示持续时间为 2秒
                                duration: 2000
                            })
                        }
                    }).catch((err)=>{
                    	console.log('request fail', err);
                    })
                })
            },
            formReset: function(e) {
                 this.formData.bookCode=''
            }
        }
    }
</script>

<style>
    .uni-form-item .title {
        padding: 20rpx 0;
    }

    .is-hover {
        color: rgba(255, 255, 255, 0.6);
        background-color: #179b16;
        border-color: #179b16;
    }
</style>